package net.cmkj.mine.bean;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import net.cmkj.mine.bean.base.BaseDictData;
import net.cmkj.mine.common.model.Condition;
import net.cmkj.mine.common.model.InvokeResult;
import net.cmkj.mine.common.model.Operators;
import net.cmkj.mine.util.CommonUtils;


/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class DictData extends BaseDictData<DictData> {
	
	public static DictData dao = new DictData().dao();
	
	public Page<DictData> getList(int pageNumber, int pageSize, String name, String typeValue, LinkedHashMap<String,String> orderbyStr) throws Exception {
		Set<Condition> conditions=new HashSet<Condition>();
		if(CommonUtils.isNotEmpty(name)){
			conditions.add(new Condition("name", Operators.LIKE, name));
		}
		conditions.add(new Condition("type_value", Operators.EQ, typeValue));
		return dao.getPage(pageNumber, pageSize, conditions, orderbyStr);
	}

	public InvokeResult save(Long id, String name, String value,
			Integer seq, String des, String typeValue) throws Exception {
		DictData dictData = new DictData();
		dictData.setId(id);
		dictData.setName(name);
		dictData.setValue(value);
		dictData.setSeq(seq);
		dictData.setDes(des);
		dictData.setType_value(typeValue);
		if(id!=null){
			dictData.update();
		}else{
			dictData.save();
		}
		return InvokeResult.success();
	}

	public InvokeResult setVisible(Long id, Integer visible) throws Exception {
		DictData dictData=this.findById(id);
		dictData.set("status", visible).update();
		return InvokeResult.success();
	} 
	
	public List<Record> getData(String typeValue) throws Exception{
		return Db.find("select id,name,value from dict_data where status=1 and type_value=? order by seq",typeValue);
	}
	
	public int getDataNum(String typeValue, String value) throws Exception {
		return Db.queryInt("select count(t.id) from dict_data t where t.status = 1 and t.type_value = ? and t.value = ?", new Object[] {typeValue, value});
	}

	public String getDataName(String typeValue,int value) throws Exception{
		Record record = Db.findFirst("select name from dict_data where status=1 and type_value=? and value=? order by seq",typeValue,value);
		if (record != null) {
			return record.get("name");
		} else {
			return "";
		}
	}
}
